import json
import requests
from third_services.WalMart.erp_get_token import get_token_by_erp



class WalmartAPI:

    # 获取单个item
    @staticmethod
    def get_one_item(ordersourcetype, OrderSourceId, source_sku):
        hearders = {
            "WM_QOS.CORRELATION_ID": "Irobotbox",
            "WM_SVC.NAME": "Walmart Marketplace",
            "Accept": "application/json",
            "Content-Type": "application/json",
            "WM_SEC.ACCESS_TOKEN": get_token_by_erp(ordersourcetype, OrderSourceId)
        }
        url = f"""https://marketplace.walmartapis.com/v3/items/{source_sku}?productIdType=SKU"""
        data = {'rowFilters': None, 'excludeColumns': None}
        res = requests.get(url=url, headers=hearders,json=data)
        sku = res.json().get('ItemResponse')[0].get('sku')
        status = res.json().get('ItemResponse')[0].get('lifecycleStatus')
        if res.status_code == 200:
            return json.dumps(res.json(),indent=4)
        else:
            print(f"接口status_code！=200 res={sku,status}")


    # 获取某渠道all item
    @staticmethod
    def get_all_item(ordersourcetype, OrderSourceId):
        hearders = {
            "WM_QOS.CORRELATION_ID": "Irobotbox",
            "WM_SVC.NAME": "Walmart Marketplace",
            "Accept": "application/json",
            "Content-Type": "application/json",
            "WM_SEC.ACCESS_TOKEN": get_token_by_erp(ordersourcetype, OrderSourceId)
        }
        url = f"""https://marketplace.walmartapis.com/v3/items?offset=0&limit=200&nextCursor=*"""
        res = requests.get(url=url, headers=hearders)
        ItemResponse = res.json().get('ItemResponse')
        all_item_set = set(map(lambda x: x.get('sku'),ItemResponse))
        totalItems = res.json().get('totalItems')
        print(f"获取到{totalItems}条数据:{all_item_set}")


if __name__ == '__main__':
    source_sku = "FZ16QM136474LTU"
    # res = WalmartAPI.get_one_item(45,5496,source_sku)
    res =WalmartAPI.get_all_item(45,5496)
    print(res)

